package bidsanity



import grails.test.mixin.*
import org.junit.*
import grailserrors.ErrorCodes

/**
 * Unit tests for the Listing domain class
 */
@TestFor(Listing)
class ListingTests {
    def s = new Customer(emailAddress: 'seller@example.org', password: "testing")
    def w = new Customer(emailAddress: 'winner@example.org', password: "testing")
    void testAdd() {
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s,
                winner: w)
        l.save(failOnError: true)
        Assert.assertFalse l.hasErrors()

        Assert.assertNotNull l.name
        Assert.assertNotNull l.endDate
        Assert.assertNotNull l.startingBidPrice
        Assert.assertNotNull l.description
        Assert.assertNotNull l.seller
        Assert.assertNotNull l.winner
        Assert.assertEquals Listing.list().size(), 1
    }

    void testNotNullFields() {
        def l = new Listing(
                name:  null,
                endDate:  null,
                seller: null,
                startDate: null
        )
        l.save(failOnError:false)
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'name', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'endDate', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'seller', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'startingBidPrice', ErrorCodes.MIN_NOT_MET)
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'startDate', ErrorCodes.NULLABLE)
    }

    void testInvalidEndDate() {
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() - 10,   //FAIL ON THIS
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s)
        l.save(failOnError: false)
        Assert.assertTrue l.hasErrors()
        Assert.assertEquals Listing.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'endDate', ErrorCodes.MIN_NOT_MET)
    }

    void testTooLongName() {
        def l = new Listing(
                //FAIL ON THIS
                name: '1234567890123456789012345678901234567890123456789012345678901234567890', //70 chars
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s)
        l.save(failOnError: false)
        Assert.assertTrue l.hasErrors()
        Assert.assertEquals Listing.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'name', ErrorCodes.TOO_BIG)
    }

    void testBlankName() {
        def l = new Listing(
                name: '', //FAIL ON THIS
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s)
        l.save(failOnError: false)
        Assert.assertTrue l.hasErrors()
        Assert.assertEquals Listing.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'name', ErrorCodes.BLANK)
    }

    void testLongDescription() {
        def l = new Listing(
                name: 'Test',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                //FAIL ON THIS
                description: '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456',
                seller: s)
        l.save(failOnError: false)
        Assert.assertTrue l.hasErrors()
        Assert.assertEquals Listing.list().size(), 0
        Assert.assertTrue ErrorCodes.CheckErrorCode(l, 'description', ErrorCodes.TOO_BIG)
    }

}
